/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clockanimation;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author trn
 */
public class Time
{
  private String systemTime;
  private Thread t;
  private Display d;
  private static final String TIME_FORMAT = "dd.MM.yyyy HH.mm.ss";

  public Time(Display d)
  {
    this.d = d;
    systemTime = new SimpleDateFormat(TIME_FORMAT).format(new Date());
    t = new Thread(new TickTack());
    startTime();
  }

  private void startTime()
  {
    t.start();
  }

  public class TickTack implements Runnable
  {
    public void run()
    {
      while(true)
      {
        String tempTime = new SimpleDateFormat(
            "dd.MM.yyyy HH:mm:ss").format(new Date());
        if(!tempTime.equals(systemTime))
        {
          systemTime = tempTime;
          d.getCanvas().showTime();
        }
      }
    }

  }

  public String getDateString()
  {
    String[] timeComps = systemTime.split(" ");
    return timeComps[0];
  }

  public String getTimeString()
  {
    String[] timeComps = systemTime.split(" ");
    return timeComps[1];
  }

  public int getHours()
  {
    String[] timeComps = getTimeString().split(":");
    return Integer.parseInt(timeComps[0]);
  }

  public int getMinutes()
  {
    String[] timeComps = getTimeString().split(":");
    return Integer.parseInt(timeComps[1]);
  }

  public int getSeconds()
  {
    String[] timeComps = getTimeString().split(":");
    return Integer.parseInt(timeComps[2]);
  }

}
